//
//  AXFMyOrderViewModel.m
//  AIXF
//
//  Created by zhanghui on 2017/7/14.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFMyOrderViewModel.h"
#import "AXFOrderManager.h"

@implementation AXFMyOrderViewModel

IMPLEMENTATION_SINGLETON(AXFMyOrderViewModel);

////发送网络请求，
//- (NSArray<AXFMyOrderViewModel *> *)orderViewModel{
//    
//    //发起网络通知
//    [self requestOrderDataWithSussessBlock:^(NSArray<AXFMyOrderViewModel *> *tempArr) {
//        
//        _orderViewModel = tempArr;
//        
//    }];
//    
//    return _orderViewModel;
//    
//}

+ (void)load
{
    [_MyOrderVieModel requestOrderDataWithSussessBlock:nil];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
//      [self requestOrderDataWithSussessBlock:^(NSArray<AXFMyOrderViewModel *> *tempArr) {
//          
//          self.orderViewModel = tempArr;
//          
//        }];
    }
    return self;
}

#pragma mark - 发送网络请求，获取数据转成ViewModel
- (void)requestOrderDataWithSussessBlock:(void(^)(NSArray <AXFMyOrderViewModel *>* viewModels))sussBlock{
    
    NSMutableArray *tempArrM = [NSMutableArray array];
    
    //发送网络请求对象
    AXFOrderManager *order = [AXFOrderManager new];
    
    __weak typeof(self) weakSelf = self;
    
    //发送网络请求
    [order requesMyOrderWithSuperMarketWith:^(id response) {
        
        
        //字典转模型
        NSArray *modelArr = [NSArray yy_modelArrayWithClass:[AXFMyOrederModel class] json:response];
     
        //遍历模型数组，创建viewmodel
        for (AXFMyOrederModel *model in modelArr) {
            
            //创建ViewModel
            AXFMyOrderViewModel *viewModel = [AXFMyOrderViewModel new];
            
            viewModel.orderModel = model;
            
            [tempArrM addObject:viewModel];
        }
    
        weakSelf.orderViewModel = tempArrM;
        //执行成功的回调
        if (sussBlock)  sussBlock(tempArrM);
        
        //发送通知
//        [[NSNotificationCenter defaultCenter]postNotificationName:DidFinishLoadDataNotifitation object:self];
        
        
    } andFailBlock:^(NSError *error) {
        
        NSLog(@"error:%@",error);
        
    }];
    
    
}



@end
